package com.hhck.controller;

import com.hhck.constants.Constants;
import com.hhck.entity.CurrentUser;
import com.hhck.entity.Page;
import com.hhck.entity.Purchase;
import com.hhck.entity.Result;
import com.hhck.service.PurchaseService;
import com.hhck.service.StoreService;
import com.hhck.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 添加采购单（商品采购）
     */
    @PostMapping("purchase-add")
    public Result purchaseAdd(@RequestBody Purchase purchase) {
        return purchaseService.savePurchase(purchase);
    }

    /**
     * 查询所有仓库
     */
    @GetMapping("/store-list")
    public Result storeList() {
        return Result.ok(storeService.queryAllStore());
    }

    /**
     * 分页查询采购单列表（条件查询）
     */
    @GetMapping("/purchase-page-list")
    public Result purchasePageList(Page page, Purchase purchase) {
        return Result.ok(purchaseService.queryPurchaseListByPage(page, purchase));
    }

    /**
     * 删除采购单
     */
    @DeleteMapping("/purchase-delete/{buyId}")
    public Result purchaseDelete(@PathVariable Integer buyId) {
        return purchaseService.removePurchaseByBuyId(buyId);
    }

    /**
     * 修改采购单
     */
    @PutMapping("/purchase-update")
    public Result purchaseUpdate(@RequestBody Purchase purchase) {
        return purchaseService.modifyPurchaseByBuyId(purchase);
    }

    /**
     * 生成入库单
     */
    @PostMapping("/in-warehouse-record-add")
    public Result inWareHouseRecordAdd(@RequestBody Purchase purchase, @RequestHeader(Constants.HEADER_TOKEN_NAME) String token) {

        // 通过token解析当前用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        return purchaseService.saveInStore(purchase, currentUser.getUserId());
    }

}

